package it.terry.pressione.hardware;

import it.terry.pressione.hardware.Sensor;
import it.terry.pressione.hardware.SensorManagerSimulator;
import android.content.Context;
import android.hardware.SensorManager;
import android.os.Build;
import android.widget.Toast;

public class SensorManagerSimulator {

	private static SensorManagerSimulator instance;
	
	@SuppressWarnings("unused")
	private static final String TAG = "SensorManagerSimulator";
	

	
	@SuppressWarnings("unused")
	private SensorManager mSensorManager = null;
	private Context mContext;
	
	public static final int SENSOR_DELAY_FASTEST = 0;
	public static final int SENSOR_DELAY_GAME = 1;
	public static final int SENSOR_DELAY_NORMAL = 3;
	public static final int SENSOR_DELAY_UI = 2;
	
	private static Sensor sensors;
	
	//Costruttore
	private SensorManagerSimulator(Context context, SensorManager systemsensormanager){
		mContext = context;
		mSensorManager = systemsensormanager;
		
	}
	
	
	public static SensorManagerSimulator getSystemService(Context context, String sensorManager) {
		if (instance == null) {
			if (sensorManager.equals(Context.SENSOR_SERVICE)) {
				if (SensorManagerSimulator.isRealSensorsAvailable()) {
					instance = new SensorManagerSimulator(context, (SensorManager)context.getSystemService(sensorManager));
				}
				else {
					instance = new SensorManagerSimulator(context, null);
					Toast.makeText(
							context, "Android SensorManager disabled, 1.5 SDK emulator crashes when using it... Make sure to connect SensorSimulator", Toast.LENGTH_LONG).show();	
				
				}
				
			}
		}
		return instance;
	}
	
	//Setta il SensorManager di Default
	public void setDefaultSensorManager(SensorManager sensormanager){
		mSensorManager = sensormanager;
	}
	
	
	private static boolean isRealSensorsAvailable() {
		if (Build.VERSION.SDK.equals("3")) {
			// We are on 1.5 SDK
			if (Build.MODEL.contains("sdk")) {
				// We are on Emulator
				return false;
			}
		}
		return true;
	}

	
	//Registra il sensore
public Sensor getDefaultSensor(int type){
		if (sensors == null){
			sensors = new Sensor(mContext, type);
			return sensors;
		}else if (sensors.get_status() == false){
			sensors.enable_pres();
			return sensors;
		}else{
			sensors.disable_temp();
			return sensors;
		}
	}
	
	
}
